﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Admin/AdminIndexMaster.master" AutoEventWireup="true" CodeFile="Admin_Spec.aspx.cs" Inherits="Admin_Admin_Spec" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    <link href="Css/Css/specification/skin_0.css" rel="stylesheet" type="text/css" />
    <script src="../Skin/CommStyle/js/jquery-1.8.3.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(function () {
            var p = '<%=lblParam %>';
            if (p == "spec") {
                $("#pageSpec_1").show();
                $("#pageSpec_2").hide();
            } else if (p == "edit") {
                $("#pageSpec_1").hide();
                $("#pageSpec_2").show();
                $("#pageSpec_2 .tab-base").html("");
                $("#pageSpec_2 .tab-base").append("<li><a  href='Admin_Spec.aspx?op=spec'><span>管理</span></a></li><li><a href='Admin_Spec.aspx?op=spec_add'><span>新增</span></a></li><li><a class='current' href='JavaScript:void(0);'><span>编辑</span></a></li>");
            } else {
                $("#pageSpec_1").hide();
                $("#pageSpec_2").show();
            }
        });
    </script>
    <div class="page" id="pageSpec_1">
        <div class="fixed-bar">
            <div class="item-title">
                <h3>
                    规格管理</h3>
                <ul class="tab-base">
                    <li><a class="current" href="JavaScript:void(0);"><span>管理</span></a></li>
                    <li><a href="Admin_Spec.aspx?op=spec_add"><span>新增</span></a></li>
                </ul>
            </div>
        </div>
        <table id="prompt" class="table tb-type2">
            <tbody>
                <tr class="space odd">
                    <th colspan="12" class="nobg">
                        <div class="title">
                            <h5>
                                操作提示</h5>
                            <span class="arrow" id="arrow"></span>
                        </div>
                    </th>
                </tr>
                <tr class="odd" id="oddarrow">
                    <td>
                        <ul>
                            <li>规格将会对应到商品发布的规格，请详细添加。如：颜色;军绿色、天蓝色、巧克力色、桔色、浅灰色、浅绿色、浅黄色等等。</li>
                            <li>当管理员添加类型时需选择规格，不同的类型可以选择相同的规格。</li>
                        </ul>
                    </td>
                </tr>
            </tbody>
        </table>
        <table class="table tb-type2">
            <thead>
                <tr class="thead">
                    <th>
                    </th>
                    <th>
                        排序
                    </th>
                    <th>
                        规格名称
                    </th>
                    <th>
                        规格值
                    </th>
                    <th class="align-center">
                        操作
                    </th>
                </tr>
            </thead>
            <tbody>
                <asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand">
                    <ItemTemplate>
                        <tr class="hover edit">
                            <td class="w24">
                                <input type="checkbox" size="6" class="checkitem" value='<%#Eval("sp_id") %>' id="itemsbox"
                                    runat="server" />
                            </td>
                            <td class="w48 sort">
                                <input type="text" maxlength="6" class="tooltip editable" title="可编辑" value='<%#Eval("sp_sort")%>' />
                            </td>
                            <td class="w270 name">
                                <span>
                                    <%#Eval("sp_name")%></span>
                            </td>
                            <td class="name">
                                <%#GetValueNameBySpID(Eval("sp_id"))%>
                            </td>
                            <td class="w96 align-center">
                                <a href='Admin_Spec.aspx?op=spec_edit&sp_id=<%#Eval("sp_id") %>'>编辑</a>
                                <asp:Literal ID="lp" runat="server">|</asp:Literal>
                                <asp:LinkButton ID="linkDel" runat="server" CommandName="del" CommandArgument='<%#Eval("sp_id") %>'
                                    OnClientClick="return confirm('确定要删除吗?')">删除</asp:LinkButton>
                            </td>
                        </tr>
                    </ItemTemplate>
                </asp:Repeater>
            </tbody>
            <tfoot>
                <tr>
                    <td>
                        <input type="checkbox" class="checkall" id="checkallBottom" value="check" onclick="checkAll(this)" />
                    </td>
                    <td id="dataFuncs" colspan="4">
                        <label for="checkallBottom">
                            全选</label>
                        <asp:LinkButton ID="linkDelete" runat="server" CssClass="btn" OnClientClick="return checkop()"
                            OnClick="linkDelete_Click"><span>删除</span> </asp:LinkButton>
                        <div class="pagination">
                            <%--<ul><li><span>首页</span></li><li><span>上一页</span></li><li><span class="currentpage">1</span></li><li><span>下一页</span></li><li><span>末页</span></li></ul>--%>
                            <ul>
                                <asp:Literal ID="ltrPage" runat="server"></asp:Literal></ul>
                        </div>
                    </td>
                </tr>
            </tfoot>
        </table>
    </div>
    <script type="text/javascript">
        //以“全选”参数复选框作为参数
        function checkAll(chk) {
            var inputs = document.getElementsByTagName("input");

            //循环判断是否是复选框
            for (var i = 0; i < inputs.length; i++) {
                if (inputs[i].type == "checkbox" && inputs[i].value != "check" && inputs[i].value != 1) {
                    inputs[i].checked = chk.checked;
                }
            }
        }
        function checkop() {
            var checkok = false;
            var e = document.getElementsByTagName("input");
            for (var i = 0; i < e.length; i++) {
                if (e[i].type == "checkbox" && e[i].value != "check") {
                    if (e[i].checked == true) {
                        checkok = true;
                        break;
                    }
                }
            }
            if (checkok)
                return confirm('删除后不可以恢复！确定要删除吗？');
            else {

                alert("至少选择一个!");
                return false;
            }
        }
        //修改序号
        function UpdateSort(id, sort) {

            $.post("../WebService/ShopService.ashx", { 'Action': 'GetUpdateSpecSort', 'id': id, 'sort': sort }, function (data, state) {
                if (state == "success") { alert("修改成功"); }
            }, true);
        }
        $(function () {
            $("#arrow").click(function () {
                if ($("#oddarrow").attr("style") == undefined || $("#oddarrow").attr("style") == "display: table-row;") {
                    $("#oddarrow").hide();
                    $("#arrow").removeClass("up");
                } else {
                    $("#oddarrow").show();
                    $("#arrow").addClass("up");
                }
            });
        });
    </script>
    <!---添加和添加页面-->
    <div class="page" id="pageSpec_2" style="display: none;">
        <div class="fixed-bar">
            <div class="item-title">
                <h3>
                    规格管理</h3>
                <ul class="tab-base">
                    <li><a href="Admin_Spec.aspx?op=spec"><span>管理</span></a></li>
                    <li><a class="current" href="JavaScript:void(0);"><span>新增</span></a></li>
                </ul>
            </div>
        </div>
        <table class="table tb-type2">
            <tbody>
                <tr class="noborder">
                    <td class="required" colspan="2">
                        <label class="validation" for="s_name">
                            规格名称</label>
                    </td>
                </tr>
                <tr class="noborder">
                    <td class="vatop rowform">
                        <input type="text" class="txt" name="s_name" id="s_name" maxlength="100" runat="server" />
                    </td>
                    <td class="vatop tips">
                        请填写常用的商品规格的名称；例如：颜色；尺寸等。
                    </td>
                </tr>
                <tr>
                    <td class="required" colspan="2">
                        <label class="validation" for="s_sort">
                            排序</label>
                    </td>
                </tr>
                <tr class="noborder">
                    <td class="vatop rowform">
                        <input type="text" class="txt" name="s_sort" maxlength="6" id="s_sort" value="0"
                            runat="server" />
                    </td>
                    <td class="vatop tips">
                        请填写自然数。规格列表将会根据排序进行由小到大排列显示。
                    </td>
                </tr>
                <tr>
                    <td class="required" colspan="2">
                        <label>
                            规格类型</label>
                    </td>
                </tr>
                <tr class="noborder">
                    <td class="vatop rowform">
                        <ul>
                            <li>
                                <asp:RadioButton ID="s_dtype_text" runat="server" Checked="true" GroupName="rdo" />
                                <label for="s_dtype_text">
                                    文字</label>
                            </li>
                            <li>
                                <asp:RadioButton ID="s_dtype_image" runat="server" GroupName="rdo" />
                                <label for="s_dtype_image">
                                    图片</label>
                            </li>
                        </ul>
                    </td>
                    <td class="vatop tips">
                        图片类型的规格值用于直观表现某些特殊规格类型，例如颜色规格，可用对应的色彩图片来表现其各种颜色值。
                    </td>
                </tr>
            </tbody>
        </table>
        <table class="table tb-type2">
            <thead class="thead">
                <tr class="space">
                    <th colspan="15">
                        <label>
                            添加规格值</label>
                    </th>
                </tr>
                <tr class="noborder">
                    <th>
                        排序
                    </th>
                    <th>
                        规格值
                    </th>
                    <th class="image_display">
                        规格图片
                    </th>
                    <th>
                    </th>
                    <th class="align-center">
                        操作
                    </th>
                </tr>
            </thead>
            <script src="Js/ajaxfileupload.js" type="text/javascript"></script>
            <script type="text/javascript">
                function ajaxFileUpload(action, spid, fileid) {
                    var srcPath = '<%=Request.Url.ToString().Remove(Request.Url.ToString().IndexOf(Request.Url.AbsolutePath)) %>';
                    var fileN = $("#" + fileid).val();
                    $.ajaxFileUpload(
                               {
                                   url: '../WebService/UploadSigleImg.ashx?action=' + action + "&spid=" + spid,            //需要链接到服务器地址
                                   secureuri: false,
                                   fileElementId: fileid,                        //文件选择框的id属性
                                   dataType: 'text',                                     //服务器返回的格式，可以是json
                                   success: function (data, status)            //相当于try语句块的用法
                                   {
                                       var _uploadF = $("#" + fileid);
                                       var _src = data.toLowerCase().replace("<pre>", "").replace("</pre>", "");
                                       _uploadF.siblings("input[type='hidden']").val(_src);
                                       _uploadF.parent().siblings().find("img").attr("src", srcPath + "/" + _src);

                                       $("#" + fileid).siblings("input[name='textfield']").val(fileN.substring(fileN.lastIndexOf("\\") + 1));

                                   },
                                   error: function (data, status, e)            //相当于catch语句块的用法
                                   {
                                       alert("上传失败！");
                                       return;
                                   }
                               }

                           );

                }

                function ClickFileUpload(id) {
                    $("#" + id).click();
                }

                function SubmitData() {
                    var spId = '<%=SpID %>';
                    var action = '<%=OAction %>';
                    if (!CheckNull()) {
                        return;
                    }
                    var ggname = $('#<%=s_name.ClientID %>').val();
                    var ggsort = $('#<%=s_sort.ClientID %>').val();
                    var $trmodel = $("#tr_model");

                    if ($('#<%=s_dtype_text.ClientID %>').attr("checked")) {
                        //只有文本，没有图片
                        var textVal = '';
                        $trmodel.children("tr").each(function () {
                            var _imgP = $(this).children("td").eq(2).children(".type-file-box").children("input[type='hidden']").val();
                            textVal += $(this).children("td").eq(0).children("input").val() + "の" + $(this).children("td").eq(1).children("input").val() + "の" +
                                           $(this).children("td").eq(2).children(".type-file-box").attr("primeryKey") + "のnullのの";

                        });
                        if (textVal != "") {
                            textVal = textVal.substring(0, textVal.length - 2);
                        }
                        $.post("../WebService/ShopService.ashx", { 'Action': 'EditSpec', 'GGName': ggname, 'GGSort': ggsort, 'GGValues': textVal, 'Format': 'text', 'SPID': spId, 'Oprate': action },
                                       function (data, state) {
                                           if (state == "success") {
                                               if (data == "1") {
                                                   alert("操作成功!");
                                                   if (action == "spec_add") {
                                                       location.href = "Admin_Spec.aspx?t=" + new Date().getDate();
                                                   } else {

                                                       location.href = location.href + "&t=" + new Date().getDate();
                                                   }


                                               } else {

                                                   alert("操作失败!");
                                               }
                                           }
                                       });
                    } else {
                        //有文本，有图片
                        var imgValue = "";
                        $trmodel.children("tr").each(function () {
                            var _imgP = $(this).children("td").eq(2).children(".type-file-box").children("input[type='hidden']").val();
                            imgValue += $(this).children("td").eq(0).children("input").val() + "の" + $(this).children("td").eq(1).children("input").val() + "の" +
                                           $(this).children("td").eq(2).children(".type-file-box").attr("primeryKey") + "の" +
                                            (_imgP == "" ? "null" : _imgP) + "のの";

                        });

                        if (imgValue != "") {
                            imgValue = imgValue.substring(0, imgValue.length - 2);
                        }

                        $.post("../WebService/ShopService.ashx", { 'Action': 'EditSpec', 'GGName': ggname, 'GGSort': ggsort, 'GGValues': imgValue, 'Format': 'image', 'SPID': spId, 'Oprate': action },
                                       function (data, state) {
                                           if (state == "success") {
                                               if (data == "1") {

                                                   alert("操作成功!");
                                                   if (action == "spec_add") {
                                                       location.href = "Admin_Spec.aspx?t=" + new Date().getDate();
                                                   } else {
                                                       location.href = location.href + "&t=" + new Date().getDate();
                                                   }


                                               } else {

                                                   alert("操作失败!");
                                               }
                                           }
                                       });
                    }


                }

                function CheckNull() {
                    if ($('#<%=s_name.ClientID %>').val() == "") {
                        alert('规格名称不能为空！');
                        return false;
                    }

                    if ($('#<%=s_sort.ClientID %>').val() == "") {
                        alert('排序不能为空！');
                        return false;
                    }
                    var $trmodel = $("#tr_model");
                    $trmodel.children("tr").children(".w270").children("input").each(function () {
                        if ($(this).val() == "") {
                            alert('规格值不能为空！');
                            return false;
                        }
                    });
                    return true;
                }

                $(function () {

                });
            </script>
            <tbody id="tr_model">
                <asp:Repeater ID="rptSpecValue" runat="server">
                    <ItemTemplate>
                        <tr class="hover edit">
                            <td class="w48 sort">
                                <input type="text" maxlength="6" value="<%#Eval("sp_value_sort") %>" id="sort_<%#Eval("sp_value_id") %>" />
                            </td>
                            <td class="name w270">
                                <input type="text" maxlength="100" value="<%#Eval("sp_value_name") %>" id="value_<%#Eval("sp_value_id") %>" />
                            </td>
                            <td class="image_display vatop rowform w300" style="display: <%#IsView() %>">
                                <span class="type-file-show">
                                    <img src="../<%#Eval("sp_value_image") %>" id="img_<%#Eval("sp_value_id") %>" width="16"
                                        height="16" class="low_source" />
                                </span><span class="type-file-box" primerykey="<%#Eval("sp_value_id") %>">
                                    <input type="text" name="textfield" id="hiden_<%#Eval("sp_value_id") %>" class="type-file-text"
                                        value="" />
                                    <input type="button" name="button" value="" class="type-file-button" onclick="ClickFileUpload('File_<%#Eval("sp_value_id") %>');" />
                                    <input id="File_<%#Eval("sp_value_id") %>" name="File_<%#Eval("sp_value_id") %>"
                                        class="type-file-file" onchange="ajaxFileUpload('<%=OAction %>','<%=SpID %>','File_<%#Eval("sp_value_id") %>')"
                                        style="width: 256px;" type="file" />
                                    <input id="Hidden_<%#Eval("sp_value_id") %>" value="<%#Eval("sp_value_image") %>"
                                        type="hidden" /></span>
                            </td>
                            <td>
                            </td>
                            <td class="w150 align-center">
                                <a onclick="remove_spec($(this),<%#Eval("sp_value_id") %>);" href="JavaScript:void(0);">
                                    移除</a>
                            </td>
                        </tr>
                    </ItemTemplate>
                </asp:Repeater>
            </tbody>
            <tbody>
                <tr>
                    <td colspan="15">
                        <a class="btn-add marginleft" id="add_spec_value" href="JavaScript:void(0);"><span>添加一个规格值</span></a>
                    </td>
                </tr>
            </tbody>
            <tfoot>
                <tr class="tfoot">
                    <td colspan="15">
                        <input id="SubmitBtn" type="button" onclick="SubmitData()" class="btn" value="提交" />
                    </td>
                </tr>
            </tfoot>
        </table>
    </div>
    <script type="text/javascript">
        //
        aotoAdd = 100;
        $(function () {

            var i = 0;


            $("#add_spec_value").click(function () {
                var tr_model = '<tr class="hover edit">' +
		                        '<td class="w48 sort"><input type="text"  maxlength="6" name="s_value[key][sort]" value="0" /></td>' +
		                        '<td class="w270 name"><input type="text"  maxlength="100" name="s_value[key][name]" value="" /></td>' +
		                        '<td class="w300 image_display vatop rowform" style="display:' + ($('#<%=s_dtype_text.ClientID %>').attr("checked") ? 'none' : 'block') + '">' +
			                        '<span class="type-file-show">' +
				                        '<img class="low_source" width="16" height="16" src="Images/Shop/transparent.gif">' +
			                        '</span>' +
			                        '<span class="type-file-box" primeryKey="0">' +
				                        '<input type="text" name="textfield" class="type-file-text" /><input type="button" name="button" value="" class="type-file-button" />' +
				                        '<input class="type-file-file" type="file" title="" id="File_a' + (aotoAdd++) + '" name="s_value_key">' +
			                        '<input value=""  type="hidden" /></span></span>' +
		                        '</td>' +
		                        '<td></td><td class="w150 align-center"><a onclick="remove_tr($(this));" href="JavaScript:void(0);">移除</a></td>' +
	                        '</tr>';

                if ($('#tr_model tr').length > 0) {
                    $('#tr_model > tr:last').after(tr_model.replace(/key/g, i));
                    if ($('.image_display').is(":hidden")) {
                        $('.image_display').hide();
                    }
                }
                else {
                    $('#tr_model').append(tr_model.replace(/key/g, i));
                    if ($('.image_display').is(":hidden")) {
                        $('.image_display').hide();
                    }
                }
                var $span = $('#tr_model > tr:last').children("td").children(".type-file-box");
                $span.children("input[type='button']").click(function () {
                    ClickFileUpload('File_a' + (aotoAdd));
                });
                $span.children("input[type='file']").change(function () {
                    ajaxFileUpload('<%=OAction %>', '<%=SpID %>', $(this).attr("id"));
                });
                $.getScript("Js/admincp.js");
                i++;
            });
            //规格图片显示与隐藏操作

            $('#<%=s_dtype_image.ClientID %>').click(function () {
                $('.image_display').show();
            });
            $('#<%=s_dtype_text.ClientID %>').click(function () {
                $('.image_display').hide();
            });
        });
        function remove_tr(o) {
            o.parents('tr:first').remove();
        }
        function remove_spec(o, spvalueid) {
            $.post("../WebService/ShopService.ashx", { 'Action': 'DeleteSpec', 'spvalueid': spvalueid },
        function (data, state) {
            if (state == "success") {
                if (data == "1") {
                    o.parents('tr:first').remove();
                }
                else {
                    alert("删除失败！");
                }
            }
        });
        }
    </script>
</asp:Content>
